<?php

/**
 * DesignController is the default controller for this application
 */
class DesignController extends BrowseControllerBase {
	/**
	 * TemplateDataService Object
	 *
	 * @var TemplateDataService object
	 */
	private $templateDataService = null;
	
	/**
	 *
	 * Template Category Data Service
	 * @var TemplateCategoryDataService object
	 */
	private $templateCategoryDataService = null;
	
	/**
	 * TemplatePhotoDataService Object
	 *
	 * @var TemplatePhotoDataService object
	 */
	private $templatePhotoDataService = null;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		// Initialize TemplateDataService
		$this->templateDataService = new TemplateDataService ();
		$this->templateCategoryDataService = new TemplateCategoryDataService ();
		$this->templatePhotoDataService = new TemplatePhotoDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.     *
	 * @return void
	 */
	
	public function indexAction() {
		$catId = $this->getQuery ( 'catId', '' );
		
		$arrElement = array ();
		$arrElement ['status'] = TemplateEntity::ACTIVE;
		if ($catId != "") {
			$arrElement ['categoryId'] = $catId;
		}
		
		$numRecordPerPage = ITEM_PER_PAGE;
		
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->templateDataService->getTotals ( $arrElement );
		$itemList = $this->templateDataService->getItems ( $arrElement, " createdDate DESC ", $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Browse/Design/Index";
		$link .= ! empty ( $catId ) ? "/catId/" . $catId : "";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		// Get Temlate Category
		$catList = $this->templateCategoryDataService->getItems ( array (), " name " );
		$this->smarty->assign ( 'catList', $catList );
		$this->smarty->assign ( 'templatePhotoDataService', $this->templatePhotoDataService );
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Design/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
